package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.beans.Transient;
import java.util.ArrayList;
import java.util.List;

@Service

public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper flavorMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    /**
     * 新增菜品
     * @param dishDTO
     * @rteurn
     */
    @Override
    @Transactional

    public void add(DishDTO dishDTO) {
        // 创建菜品对象
        Dish dish  = new Dish();
        BeanUtils.copyProperties(dishDTO,dish); // 拷贝属性
        // 保存菜品
        dishMapper.insert(dish);
        // 获取菜品id
        Long dishId = dish.getId();
        // 保存口味
        // 获取口味数据
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if(!flavors.isEmpty()) {
            // 遍历集合，插入id值
            for(DishFlavor flavor : flavors){
                flavor.setDishId(dishId);
            }
            flavorMapper.insertBatch(flavors);
        }

    }

    @Override
    public PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());
        Page<DishVO> dishVOPage = dishMapper.pageQuery(dishPageQueryDTO);
        return new PageResult(dishVOPage.getTotal(), dishVOPage.getResult());
    }

    /**
     * 批量删除
     * @param ids
     */
    @Override
    @Transactional
    public void deleteBatch(List<Long> ids) {
        // 1.判断当前菜品是否处于启售中，如果启售则不可删除
        ids.forEach(id -> {
            Dish dish = dishMapper.getById(id);
            if(dish.getStatus() == StatusConstant.ENABLE) {
                //菜品启售，不可删除，抛出异常
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }

        });
        // 2.判断当前菜品是否与套餐关联，如果关联不可删除
        List<Long> setmealIds = setmealDishMapper.getSetmealIdsByDishIdS(ids);
        if(!setmealIds.isEmpty() && setmealIds.size() > 0) {
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }
        // 3.删除菜品
        ids.forEach(id -> {
            dishMapper.deleteById(id);
            // 4.删除口味数据
            flavorMapper.deleteByDishId(id);
        });
    }

    /**
     * 根绝id查询菜品信息
     * @param id
     * @return
     */
    @Override
    public DishVO getById(Long id) {
        // 1. 根据id查询菜品信息
        Dish dish = dishMapper.getById(id);
        // 2. 根据菜品信息查询口味数据
        List<DishFlavor> flavors = flavorMapper.getByDishId(id);
        // 3. 封装数据返回
        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish,dishVO);
        dishVO.setFlavors(flavors);
        return dishVO;
    }

    /**
     * 修改菜品信息
     * @param dishDTO
     */
    @Override
    public void update(DishDTO dishDTO) {
        // 1. 修改菜品信息
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.update(dish);
        // 2. 修改口味信息
        // 2.1 先删除原先的所有口味信息
        flavorMapper.deleteByDishId(dishDTO.getId());
        // 2.2 再插入新的口味信息
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if(!flavors.isEmpty() && flavors.size() > 0) {
            flavors.forEach(flavor -> {
                flavor.setDishId(dishDTO.getId());
            });
            flavorMapper.insertBatch(flavors);
        }

    }

    /**
     * 修改菜品状态
     * @param status
     * @param id
     */
    @Override
    public void changeStatus(Integer status, Long id) {
        Dish dish = Dish.builder().status(status).id(id).build();
        dishMapper.update(dish);
    }

    /**
     * 条件查询菜品和口味
     * @param dish
     * @return
     */
    @Override
    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.list(dish);

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = flavorMapper.getByDishId(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }

}
